﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.Text;
using System.Windows.Forms;
using System.Windows.Media;
namespace FdControlls.Fonts
{
    /// <summary>
    /// 读取字体资源文件
    /// </summary>
    public class FontRead
    {
        #region  初始化字体资源
        static FontRead()
        {
            InitialiseFontFile();
        }
        /// <summary>
        /// 字体资源
        /// </summary>
        public static readonly PrivateFontCollection fonts = new PrivateFontCollection();
        
        [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont,
         IntPtr pdv, [System.Runtime.InteropServices.In] ref uint pcFonts);
        /// <summary>
        /// 初始化字体资源
        /// </summary>
        private static void InitialiseFontFile()
        {
            try
            {
                unsafe
                {
                    fixed (byte* pFontData = FontIconDLL.Fonts.Resource1.MaterialIcons)
                    {
                        uint dummy = 0;
                        fonts.AddMemoryFont((IntPtr)pFontData, FontIconDLL.Fonts.Resource1.MaterialIcons.Length);
                        AddFontMemResourceEx((IntPtr)pFontData, (uint)FontIconDLL.Fonts.Resource1.MaterialIcons.Length, IntPtr.Zero, ref dummy);
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        #endregion

        /// <summary>
        /// 获取字体的信息 生产环境无用
        /// </summary>
        /// <returns></returns>
        public static List<string> ReadSymbol()
        {
            //var families = Fonts.GetFontFamilies(@"C:\WINDOWS\Fonts\Arial.TTF");
            var families = System.Windows.Media.Fonts.GetFontFamilies(Application.StartupPath + @"\Fonts\MaterialIcons-Regular.ttf");
            List<string> ls = new List<string>();
            foreach (System.Windows.Media.FontFamily family in families)
            {
                var typefaces = family.GetTypefaces();
                foreach (Typeface typeface in typefaces)
                {
                    GlyphTypeface glyph;
                    typeface.TryGetGlyphTypeface(out glyph);
                    IDictionary<int, ushort> characterMap = glyph.CharacterToGlyphMap;

                    int i = 0;

                    foreach (KeyValuePair<int, ushort> kvp in characterMap)
                    {
                        i++;
                        if (i < 38)
                        {
                            continue;
                        }
                       
                       // Console.WriteLine(String.Format("{0}:{1}", kvp.Key, kvp.Value));
                        ls.Add(char.ConvertFromUtf32(kvp.Key));
                    }

                    return ls;
                }
            }
            return null;
        }

        /// <summary>
        /// 生成字体对应的编码值
        /// </summary>
        private void GetEnumStr()
        {
            List<string> ls = FontRead.ReadSymbol();
            StringBuilder sb = new StringBuilder(0);
            int igz = 0;
            foreach (string s in ls)
            {
                sb.Append("ICON_")
                    .Append(igz)
                    .Append(" = ")
                    .Append(s)
                    .Append(",").Append("\r\n");
                igz++;
            }
            string sss = sb.ToString();
            System.Console.WriteLine(sss);
        }
    }
}
